package cn.com.chec.en.controller.admin;

import cn.com.chec.en.dao.ContentInfoDao;
import cn.com.chec.en.dao.UserInfoDao;
import cn.com.chec.en.dao.WebsiteInfoDao;
import cn.com.chec.en.model.domain.ContentInfo;
import cn.com.chec.en.model.domain.UserInfo;
import cn.com.chec.en.model.domain.WebsiteInfo;
import cn.com.chec.en.util.PageBean;
import cn.com.chec.en.util.StringTools;
import cn.com.chec.en.util.WebUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ContentInfoController {
	protected static final Logger logger = Logger
			.getLogger(ContentInfoController.class);

	@Resource
	private ContentInfoDao contentDao;

	@Resource
	private WebsiteInfoDao websiteInfoDao;

	@Resource
	private UserInfoDao userInfoDao;

	@RequestMapping({ "/admin/frame/contentInfo.do" })
	public ModelAndView frame(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/frame/frame_contentInfo");
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/admin/contentInfo/left.do" })
	public ModelAndView left(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/contentInfo/left");
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/admin/contentInfo/list.do" })
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/contentInfo/contentInfoList");
		int curPage = WebUtils.getRequestParameterAsInt(request, "curPage", 1);
		String kw = WebUtils.getRequestParameterAsString(request, "kw");
		int websiteId = WebUtils.getRequestParameterAsInt(request, "websiteId",
				0);
		int userId = WebUtils.getRequestParameterAsInt(request, "userId", 0);
		PageBean pageBean = new PageBean();
		pageBean.setCurPage(curPage);
		ContentInfo query = new ContentInfo();
		query.setKw(kw);
		query.setWebsiteId(websiteId);
		query.setUserId(userId);
		List<ContentInfo> contentInfoList = this.contentDao.getSubList(query,
				pageBean.getStart(), pageBean.getRowsPerPage());
		pageBean.setMaxRowCount(this.contentDao.getCount(query));
		pageBean.setMaxPage();
		pageBean.setPageNoList();

		List<UserInfo> userInfoList = this.userInfoDao.getUserListByRoleId(-1, 0,
				2147483647);
		List<WebsiteInfo> websiteInfoList = this.websiteInfoDao.getSubList(null, 0,
				2147483647);
		map.put("contentInfoList", contentInfoList);
		map.put("pageBean", pageBean);
		map.put("content", query);
		map.put("userInfoList", userInfoList);
		map.put("websiteInfoList", websiteInfoList);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping(value = { "/admin/contentInfo/showInsert.do" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	public ModelAndView showInsert(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/contentInfo/contentInfoOpr");
		ContentInfo content = new ContentInfo();
		content.setPublishTime(DateFormatUtils.format(new Date(),
				"yyyy-MM-dd HH:mm:ss"));
		List<WebsiteInfo> websiteInfoList = this.websiteInfoDao.getSubList(null, 0,
				2147483647);
		List<UserInfo> userInfoList = this.userInfoDao.getUserListByRoleId(0, 0,
				2147483647);
		map.put("act", "insert");
		map.put("content", content);
		map.put("websiteInfoList", websiteInfoList);
		map.put("userInfoList", userInfoList);
		map.put("userId", Integer.valueOf(((UserInfo) session
				.getAttribute("systemUserInfo")).getId()));
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping(value = { "/admin/contentInfo/insert.do" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	public ModelAndView insert(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		String message = "";
		ModelAndView mav = new ModelAndView("admin/common/message");

		String contentId = StringTools.getUUID();
		String title = WebUtils.getRequestParameterAsString(request, "title");
		int websiteId = 0;
		List<WebsiteInfo> websiteInfoList = this.websiteInfoDao.getSubList(null, 0,
				2147483647);
		for (WebsiteInfo website : websiteInfoList) {
			if (website.getIsSelf() == 1) {
				websiteId = website.getId();
			}
		}
		String text = WebUtils.getRequestParameterAsString(request, "text");
		String publishTime = WebUtils.getRequestParameterAsString(request,
				"publishTime");
		int userId = WebUtils.getRequestParameterAsInt(request, "userId", 0);

		if (StringUtils.isBlank(text)) {
			map.put("message", "新闻内容不能为空!");
			mav.addAllObjects(map);
			return mav;
		}

		ContentInfo content = new ContentInfo();
		content.setContentId(contentId);
		content.setTitle(title);
		content.setWebsiteId(websiteId);
		content.setText(text);
		content.setPublishTime(publishTime);
		content.setUserId(userId);
		boolean flag = this.contentDao.insert(content);
		if (flag)
			message = "新闻信息添加完毕!";
		else {
			message = "新闻信息添加失败!";
		}
		map.put("message", message);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/admin/contentInfo/showUpdate.do" })
	public ModelAndView showUpdate(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/contentInfo/contentInfoOpr");
		String contentId = WebUtils.getRequestParameterAsString(request,
				"contentId");
		ContentInfo content = this.contentDao.getContentById(contentId);
		List<WebsiteInfo> websiteInfoList = this.websiteInfoDao.getSubList(null, 0,
				2147483647);
		List<UserInfo> userInfoList = this.userInfoDao.getUserListByRoleId(0, 0,
				2147483647);

		map.put("act", "update");
		map.put("content", content);
		map.put("websiteInfoList", websiteInfoList);
		map.put("userInfoList", userInfoList);

		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping(value = { "/admin/contentInfo/update.do" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		String message = "";
		ModelAndView mav = new ModelAndView("admin/common/message");
		String contentId = WebUtils.getRequestParameterAsString(request,
				"contentId");
		String title = WebUtils.getRequestParameterAsString(request, "title");
		int websiteId = 0;
		List<WebsiteInfo> websiteInfoList = this.websiteInfoDao.getSubList(null, 0,
				2147483647);
		for (WebsiteInfo website : websiteInfoList) {
			if (website.getIsSelf() == 1) {
				websiteId = website.getId();
			}
		}
		String text = WebUtils.getRequestParameterAsString(request, "text");
		String publishTime = WebUtils.getRequestParameterAsString(request,
				"publishTime");
		int userId = WebUtils.getRequestParameterAsInt(request, "userId", 0);

		if (StringUtils.isBlank(text)) {
			map.put("message", "新闻内容不能为空!");
			mav.addAllObjects(map);
			return mav;
		}

		ContentInfo content = this.contentDao.getContentById(contentId);
		content.setTitle(title);
		content.setWebsiteId(websiteId);
		content.setText(text);
		content.setPublishTime(publishTime);
		content.setUserId(userId);

		boolean flag = this.contentDao.update(content);
		if (flag)
			message = "新闻信息更新完毕!";
		else {
			message = "新闻信息更新失败!";
		}
		map.put("message", message);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/admin/contentInfo/del.do" })
	public ModelAndView del(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		String message = "";
		ModelAndView mav = new ModelAndView("admin/common/message");
		String contentIds = WebUtils.getRequestParameterAsString(request,
				"contentId");
		String[] idArr = StringUtils.split(contentIds, ",");
		try {
			for (String contentId : idArr) {
				this.contentDao.del(contentId);
			}
			message = "新闻信息删除完毕!";
		} catch (Exception e) {
			message = "新闻信息删除失败!";
			logger.error("新闻信息删除失败! ids=" + contentIds, e);
		}
		map.put("message", message);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/admin/contentInfo/showContentSynch.do" })
	public ModelAndView showContentSynch(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView(
				"admin/contentInfo/showContentSynch");
		String contentIds = WebUtils.getRequestParameterAsString(request,
				"contentId");
		if (StringUtils.isBlank(contentIds)) {
			map.put("message", "请选择要同步的文章!");
			mav.addAllObjects(map);
			mav.setViewName("admin/common/message");
			return mav;
		}
		List<ContentInfo> contentInfoList = new ArrayList<ContentInfo>();
		String[] ids = contentIds.split(",");
		ContentInfo content = null;
		for (String contentId : ids) {
			content = this.contentDao.getContentById(contentId);
			contentInfoList.add(content);
		}
		List<WebsiteInfo> websiteInfoList = this.websiteInfoDao.getSubList(null, 0,
				2147483647);
		map.put("websiteInfoList", websiteInfoList);
		map.put("contentInfoList", contentInfoList);
		mav.addAllObjects(map);
		return mav;
	}
}